/** 
* 2011-8-18 
* StrUtils.java 
* author:xfy
*/ 

package com.hutiao.snowhb.common;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;


/**
 * @author Administrator
 *
 */
public class MyStrUtils {

	/*--------------------------------------------------------------------------------------*/
	public static String dealWord(String oldStr, String removeStr) {
		String newStr = "";
		if (StringUtils.contains(oldStr, removeStr)) {
			String temp = "";
			temp = StringUtils.remove(oldStr, removeStr);
			newStr = StringUtils.isBlank(temp) ? oldStr : temp;
		} else {
			newStr = oldStr;
		}
		return newStr;
	}
	
	public static String subWords(String oldword,String search,int index) {
		Pattern trpat=Pattern.compile(search);
		Matcher mat1=trpat.matcher(oldword);
		String temp="";
		while (mat1.find()) {
			temp=mat1.group(index);
		}
		return temp;
	}
	
	public static String getCenterStr(String oldStr,String s,String e) {
		String newStr="";
		if (StringUtils.contains(oldStr, s) && StringUtils.contains(oldStr, e)) {
			newStr=StringUtils.substringBetween(oldStr, s, e);
		}else {
			newStr=oldStr;
		}
		return newStr;
	}
	/*--------------------------------------------------------------------------------------*/
	
	
	/*--------------------------------------------------------------------------------------*/
	public static String getUrl(String str) {
		String url = "";
		String regxpForHref = "href=\"(.*?)\""; // ����������<��ͷ��>��β�ı�ǩ
		Pattern pattern = Pattern.compile(regxpForHref);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
        	url = matcher.group(1);
        }
        return url;
	}
	
	public static String wipeHtml(String str) {
		String regxpForHtml = "<([^>]*)>"; // ����������<��ͷ��>��β�ı�ǩ
		Pattern pattern = Pattern.compile(regxpForHtml);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "");
        }
        matcher.appendTail(sb);
        return sb.toString();
	}
	/*--------------------------------------------------------------------------------------*/
	
	/*--------------------------------------------------------------------------------------*/
	public static String a1ertxxx(String str, String pwd) {

		String prand = "";

		for (int i = 0; i < pwd.length(); i++) {
			prand += "" + (int) pwd.charAt(i);
		}
		// System.out.println("prand:"+prand);

		int sPos = (int) Math.floor(prand.length() / 5);
		// System.out.println("sPos:"+sPos);
		int mult = Integer.parseInt(String.valueOf(prand.charAt(sPos)) + String.valueOf(prand.charAt(sPos * 2)) + String.valueOf(prand.charAt(sPos * 3))
				+ String.valueOf(prand.charAt(sPos * 4)) + String.valueOf(prand.charAt(sPos * 5)));
		// System.out.println("mult:"+mult);

		int incr = (int) Math.ceil(pwd.length() / 2);
		// System.out.println("incr:"+incr);
		long modu = (long) Math.pow(2, 31) - 1;
		// System.out.println("modu:"+modu);
		double randorm123 = Math.random();
		// double randorm123 = 0.355152775766328;
		// System.out.println("randorm123:"+randorm123);
		int salt = (int) (Math.round(randorm123 * 1000000000) % 100000000);
		// System.out.println("salt:"+salt);
		prand += salt;

		prand = ((mult * prand.length() + incr) % modu) + "";
		// System.out.println("prand:"+prand);
		long enc_chr;
		String enc_str = "";
		for (int i = 0; i < str.length(); i++) {
			int a = (int) str.charAt(i);
			long b = (long) Math.floor((double) Long.parseLong(prand) / (double) modu * 255);
			// System.out.println("a:"+a);
			// System.out.println("b:"+b);
			enc_chr = a ^ b;
			// System.out.println("enc_chr:"+enc_chr);

			if (enc_chr < 16) {
				enc_str += "0" + toHex(enc_chr, false);
			} else
				enc_str += toHex(enc_chr, false);

			// System.out.println("enc_str:"+enc_str);

			prand = "" + (mult * Long.parseLong(prand) + incr) % modu;
			// System.out.println("prand:"+prand);
		}
		String str_salt = toHex(salt, false);
		// System.out.println("salt:"+str_salt);
		while (str_salt.length() < 8)
			str_salt = "0" + str_salt;
		// System.out.println("salt:"+str_salt);
		enc_str += str_salt;

		enc_str = enc_str.toLowerCase();

		// System.out.println("enc_str:"+enc_str);

		return enc_str;
	}

	public static String toHex(long k, boolean headOrNo) {
		StringBuffer sb = new StringBuffer("");
		String flag = "0x";
		if (k < 0) {
			flag = "-0x";
			k *= -1;
		}
		long t;

		do {
			t = k % 16;
			if (t > 9) {
				char c = 'A';
				for (int i = 0; i < t - 10; i++, c++)
					;
				sb.insert(0, c);
			} else {
				sb.insert(0, t);
			}
			k /= 16;
		} while (k > 0);
		if (headOrNo) {
			sb.insert(0, flag);
		} else {
			return sb.toString();
		}
		return sb.toString();
	}

	/*--------------------------------------------------------------------------------------*/
	
	/*--------------------------------------------------------------------------------------*/
	public static String getSpecifiedDayBefore(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);

		String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayBefore;
	}

	/**
	 * 
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayAfter(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);

		String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayAfter;
	}
	
	//处理时间   1小时32分
	public static String fomartTime(String alltime){
		String reuslt="";
		if (StringUtils.contains(alltime, "小时") && StringUtils.contains(alltime, "分")) {
			String temp1 = StringUtils.replace(alltime, "小时", ":");
			String temp2 = StringUtils.replace(temp1, "分", "");
			String s=StringUtils.substringBefore(temp2, ":");
			String e=StringUtils.substringAfter(temp2, ":");
			if (s.length()<=1) {
				temp2="0"+temp2;
			}
			if (e.length()<=1) {
				temp2=s+":0"+e;
			}
			reuslt=temp2+":00";
			
		}else if (StringUtils.contains(alltime, "分") && !StringUtils.contains(alltime, "小时")) {
			String temp2 = StringUtils.replace(alltime, "分", "");
			if (temp2.length()<=1) {
				temp2="0"+temp2;
			}
			reuslt="00:"+temp2+":00";
		}else if (!StringUtils.contains(alltime, "分") && StringUtils.contains(alltime, "小时")) {
			String temp1 = StringUtils.replace(alltime, "小时", "");
			reuslt=temp1+":00"+":00";
		}
		else {
			if (StringUtils.isBlank(alltime)) {
				reuslt="0";
			}else {
				reuslt=alltime;
			}
		}
		
		return reuslt;
	}
	/*--------------------------------------------------------------------------------------*/
	public static int compareTime(String startDateAndTime, String endDateAndTime) {
		Timestamp startDate = null;
		Timestamp endDate = null;
		try {
			startDate = java.sql.Timestamp.valueOf(startDateAndTime);
			endDate = java.sql.Timestamp.valueOf(endDateAndTime);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (startDate.before(endDate)) {
			return -1;
		} else if (startDate.after(endDate)) {
			return 1;
		} else {
			return 0;
		}
	}
	
	
	public static String removeComma(String oldString) {
		
		int index = oldString.indexOf("\"");
		if (index == -1) return oldString;
		String str1 = oldString.substring(0, index);
		String temp = oldString.substring(index+1, oldString.length());
		
		int index2 = temp.indexOf("\"");
		if (index2 == -1) return oldString;
		String str2 = temp.substring(0, index2);
		String str3 = temp.substring(index2+1, temp.length());
		
		String newString = str1+str2.replaceAll(",", "")+str3;
//		System.out.println(oldString);
//		System.out.println(newString);
//		System.out.println();
		
		return removeComma(newString);

	}
	
	
	public static String removeQuotes(String str) {
		
		if (str.startsWith("\"")) {
			str = str.substring(1, str.length());
		}
		if (str.endsWith("\"")) {
			str = str.substring(0, str.length()-1);
		}
		
		return str = str.trim();
		
	}
	
	
	public static void main(String[] args) {
		/*String thisdate="2011-09-13";
		String string=getSpecifiedDayAfter(thisdate);
		String[] strs=StringUtils.split(string, "-");
		for (String s : strs) {
			System.out.println(s);
		}*/
		
		/*String date1="1小时32分";
		String date2="3小时";
		String re1=fomartTime(date1);
		String re2=fomartTime(date2);
		System.out.println(re1+"-----------"+re2);*/
//		String nowdate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//		String daystr=StringUtils.substring(nowdate, nowdate.length()-2, nowdate.length());
//		System.out.println(daystr);
		
		//MyStrUtils.removeComma("abc,\"123,4.5\",\"6,78.9\",def");
		
		String test = "\"2013021520705766	\"";
		System.out.println(test);
//		
//		if (test.startsWith("\"")) {
//			test = test.substring(1, test.length());
//			System.out.println(test);
//		}
//		if (test.endsWith("\"")) {
//			test = test.substring(0, test.length()-1);
//			System.out.println(test);
//		}
//		
//		test = test.trim();
//		System.out.println(test);
		
		test = removeQuotes(test);
		System.out.println(test);
		
	}
}
